package com.atguigu.exam.controller;


//import com.atguigu.exam.common.Result;
//import com.atguigu.exam.entity.Banner;
import com.atguigu.exam.common.Result;
import com.atguigu.exam.entity.Banner;
import com.atguigu.exam.service.BannerService;
import com.atguigu.exam.service.FileUploadService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 轮播图控制器 - 处理轮播图管理相关的HTTP请求
 * 包括图片上传、轮播图的CRUD操作、状态切换等功能
 */
@RestController  // REST控制器，返回JSON数据
@RequestMapping("/api/banners")  // 轮播图API路径前缀
//@CrossOrigin  // 允许跨域访问
@Tag(name = "轮播图管理", description = "轮播图相关操作，包括图片上传、轮播图增删改查、状态管理等功能")  // Swagger API分组
public class BannerController {

    @Autowired
    private BannerService bannerService;

    @Autowired
    private FileUploadService fileUploadService;

    @GetMapping("/list")
    @Operation(summary = "获取所有轮播图")
    public Result<List<Banner>> getAllBanners() {
        LambdaQueryWrapper<Banner> bannerLambdaQueryWrapper = new LambdaQueryWrapper<>();
        bannerLambdaQueryWrapper.orderByAsc(Banner::getSortOrder);
        List<Banner> list = bannerService.list(bannerLambdaQueryWrapper);
        return Result.success(list);
    }

    @PostMapping("/upload-image")
    @Operation(summary = "上传轮播图图片")
    public Result uploadImage(MultipartFile  file){
           String url= fileUploadService.uploadFile(file);
           return Result.success(url,"上传成功");
    }

    @GetMapping("/active")
    @Operation(summary = "获取当前激活的轮播图")
    public Result<List<Banner>> getActiveBanners() {
        QueryWrapper<Banner> bannerQueryWrapper = new QueryWrapper<>();
        bannerQueryWrapper.eq("is_active",true);
        List<Banner> list = bannerService.list(bannerQueryWrapper);
        return Result.success(list);
    }

    @GetMapping("/{id}")
    @Operation(summary = "根据id获取轮播图")
    public Result<Banner> getBannerById(@PathVariable long id) {
        Banner byId = bannerService.getById(id);
        return Result.success(byId);
    }

    @DeleteMapping("/delete/{id}")
    @Operation(summary = "根据id删除轮播图")
    public Result deleteBanner(@PathVariable long id) {
        boolean b = bannerService.removeById(id);
        if (b) {
            return Result.success("删除成功");
        } else {
            return Result.error("删除失败");
        }
    }

//    @PostMapping("/add")
//    @Operation(summary = "添加轮播图")
//    public Result addBanner(@RequestBody Banner banner){
//        bannerService.save(banner);
//        return Result.success("添加成功");
//    }
//
//    @PutMapping("/update")
//    @Operation(summary = "更新轮播图")
//    public Result updateBanner(@RequestBody Banner banner){
//        boolean b = bannerService.updateById(banner);
//        if (b) {
//            return Result.success("更新成功");
//        } else {
//            return Result.error("更新失败");
//        }
//    }

    @RequestMapping(value ={ "/add","update"},method = {RequestMethod.POST,RequestMethod.PUT})
    @Operation(summary = "添加或更新轮播图")
    public Result addOrUpdateBanner(@RequestBody Banner banner){
        boolean b = bannerService.saveOrUpdate(banner);
        if (b) {
            return Result.success("添加或更新成功");
        } else {
            return Result.error("添加或更新失败");
        }
    }

    @PutMapping("/toggle/{id}")
    @Operation(summary = "切换轮播图状态")
    public Result toggleBanner(@PathVariable long id, @RequestParam boolean isActive) {
        UpdateWrapper<Banner> bannerUpdateWrapper = new UpdateWrapper<>();
        bannerUpdateWrapper.set("is_active",isActive).set("update_time",new Date());
        bannerUpdateWrapper.eq("id",id);
        boolean update = bannerService.update(bannerUpdateWrapper);
        if (update) {
            return Result.success("切换成功");
        } else {
            return Result.error("切换失败");
        }
    }
} 